// part1:C06:ex6.22
// swap int pointer.
#include <iostream>
using namespace std;

void swap_pointer1(int *pa, int *pb) {
  int *tmp = pa;
  pa = pb;
  pb = tmp;
}

void swap_pointer2(int *pa, int *pb) {
  int tmp = *pa;
  *pa = *pb;
  *pb = tmp;
}

void swap_pointer3(int *&pa, int *&pb) {
  int *tmp = pa;
  pa = pb;
  pb = tmp;
}

int main(void) {
  int a = 5, b = 10;
  int *pa = &a, *pb = &b;
  cout << "before : a = " << a << " b = " << b << endl;
  cout << "pa = " << pa << " pb = " << pb << endl;
  cout << "*pa = " << *pa << " *pb = " << *pb << endl;
  swap_pointer1(pa, pb);
  cout << "after: a = " << a << " b = " << b << endl;
  cout << "pa = " << pa << " pb = " << pb << endl;
  cout << "*pa = " << *pa << " *pb = " << *pb << endl;

  a = 5, b = 10;
  pa = &a, pb = &b;
  cout << "before : a = " << a << " b = " << b << endl;
  cout << "pa = " << pa << " pb = " << pb << endl;
  cout << "*pa = " << *pa << " *pb = " << *pb << endl;
  swap_pointer2(pa, pb);
  cout << "after: a = " << a << " b = " << b << endl;
  cout << "pa = " << pa << " pb = " << pb << endl;
  cout << "*pa = " << *pa << " *pb = " << *pb << endl;

  a = 5, b = 10;
  pa = &a, pb = &b;
  cout << "before : a = " << a << " b = " << b << endl;
  cout << "pa = " << pa << " pb = " << pb << endl;
  cout << "*pa = " << *pa << " *pb = " << *pb << endl;
  swap_pointer3(pa, pb);
  cout << "after: a = " << a << " b = " << b << endl;
  cout << "pa = " << pa << " pb = " << pb << endl;
  cout << "*pa = " << *pa << " *pb = " << *pb << endl;
  delete pa;
  delete pb;

  return 0;
}
